package com.slkj.shilixiaoyuanapp.ui.common;

/**
 * Created  on 2018-08-21
 *
 * @author 唐开阔
 * @describe
 */

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.MessageContactsGroupEntity;
import com.slkj.shilixiaoyuanapp.entity.contacts.ContactsBackEntity;
import com.slkj.shilixiaoyuanapp.entity.contacts.ContactsGroupEntity;
import com.slkj.shilixiaoyuanapp.entity.contacts.ContactsPeopleEntity;
import com.slkj.shilixiaoyuanapp.entity.contacts.MailListEntity;
import com.slkj.shilixiaoyuanapp.entity.contacts.PersonBean;
import com.slkj.shilixiaoyuanapp.entity.contacts.Student;
import com.slkj.shilixiaoyuanapp.net.HttpConfig;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.common.adapter.ContactsAdapter;
import com.slkj.shilixiaoyuanapp.ui.common.adapter.SendObjAdapter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.notice.NoticeReadDetailActivity;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.StateLayout;
import com.tkk.api.RxEventProcessor;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import butterknife.BindView;

import static com.slkj.shilixiaoyuanapp.entity.code.RxEventProcessorTag.CHOSE_SEND_OBJ_FINSH;

@ActivityInfo(layout = R.layout.activity_chose_obj, isShowActionBar = true,title = "发布对象", rightHint = "确定")
public class ChoseObjActivity extends BaseActivity {
    @BindView(R.id.recycer)
    RecyclerView mRecyclerView;
    ContactsAdapter adapter;
    @InjectParam
    ArrayList<ContactsPeopleEntity> contacts = new ArrayList<>();
    @InjectParam
    int maxNum;
    @InjectParam
    String tag;
    @BindView(R.id.statelayout)StateLayout stateLayout;
    @Override
    protected void init() {
        ChoseObjActivity__JumpCenter.bind(this);
        adapter = new ContactsAdapter(contacts, this, maxNum);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.VERTICAL));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        if (contacts.isEmpty()) {
            HttpHeper.get().mainPageService().getContacts()
                    .map(result -> {
                        if (HttpConfig.RESPONSE_OK.equals(result.getStatus())) {
                            ContactsBackEntity contactsBackEntity = result.getData();
                            contacts.add(contactsBackEntity.getTeacher());
                            contacts.addAll(contactsBackEntity.getStudent());
                        }
                        return result;
                    })
                    .compose(bindToLifecycle())
                    .compose(getThread())
                    .subscribe(new CommonCallBack<ContactsBackEntity>(stateLayout) {
                        @Override
                        public void onCallBackSuccess(ContactsBackEntity data) {
                            adapter.notifyDataSetChanged();
                        }
                    });
        }else {
            stateLayout.showContentView();
        }
    }

    @Override
    protected void onRightTitleClick() {
        ArrayList<ContactsPeopleEntity> group = new ArrayList<>();
        //只传递分组的数据
        for (ContactsPeopleEntity entity : contacts) {
            if (entity.getItemType() == ContactsPeopleEntity.TYPE_GROUP) {
                group.add(entity);
            }
        }
        RxEventProcessor.get().post(tag + CHOSE_SEND_OBJ_FINSH, group);
        finish();
    }

    /**
     * 获取所有被选择的数据
     *
     * @param data
     * @return
     */
    public static ArrayList<ContactsPeopleEntity> getCheckConstacts(ArrayList<ContactsPeopleEntity> data) {
        ArrayList<ContactsPeopleEntity> checkd = new ArrayList<>();
        //未展开的要遍历其子项
        for (ContactsPeopleEntity entity : data) {
            if (entity.getItemType() == ContactsPeopleEntity.TYPE_GROUP) {
                //没有展开遍历他的子项
                if (!entity.isExpand()) {
                    List<ContactsPeopleEntity> list = entity.getList();
                    for (ContactsPeopleEntity child : list) {
                        if (child.isCheck()) {
                            checkd.add(child);
                        }
                    }
                }
            } else {
                if (entity.isCheck()) {
                    checkd.add(entity);
                }
            }
        }
        return checkd;
    }

    /**
     * 从分组列表里面获取选中的项目
     *
     * @param data
     * @return
     */
    public static ArrayList<ContactsPeopleEntity> getCheckConstactsByGroup(ArrayList<ContactsPeopleEntity> data) {
        ArrayList<ContactsPeopleEntity> checkd = new ArrayList<>();
        for (ContactsPeopleEntity entity : data) {
            if (entity.isCheck()) {
                checkd.addAll(entity.getList());
            } else {
                for (ContactsPeopleEntity person : entity.getList()) {
                    if (person.isCheck()) {
                        checkd.add(person);
                    }
                }
            }

        }
        return checkd;
    }

    public static List<Integer> getIds(ArrayList<ContactsPeopleEntity> data) {
        List<Integer> ids = new ArrayList<>();
        if (data != null && !data.isEmpty()) {
            for (ContactsPeopleEntity entity : data) {
                ids.add(entity.getId());
            }
        }
        return ids;
    }

    public static String getInfos(ArrayList<ContactsPeopleEntity> data) {
        String infos = "";
        int size = data.size();
        switch (size) {
            case 1:
                infos = data.get(0).getName();
                break;
            case 2:
                infos = data.get(0).getName()+","+data.get(1).getName();

                break;
            case 3:
                infos = data.get(0).getName() + "," + data.get(1).getName()+ "," + data.get(2).getName();
                break;
            default:
                infos = data.get(0).getName()+","+data.get(1).getName()+ "," + data.get(2).getName()+"等"+size+"人";

                break;
        }
        return infos;
    }
}